/*
 * Copyright 2009 Jinbo Lin. All rights reserved.
 * 
 * cn.edu.pku.dao.impl HumanResourcesManagementSystem
 *
 * Created on 2009-12-20
 */
package cn.edu.pku.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.pku.dao.SalarystandardDao;
import cn.edu.pku.model.Salarystandard;

/**
 * @author Jinbo Lin
 * 
 * 
 */
public class SalarystandardDaoHibernate extends HibernateDaoSupport implements
		SalarystandardDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		if (null != this.get(id)) {
			this.getHibernateTemplate().delete(
					this.getHibernateTemplate().get(Salarystandard.class, id));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#delete(cn.edu.pku.model.Salarystandard)
	 */
	@Override
	public void delete(Salarystandard salarystandard) {
		if (null != this.get(salarystandard.getSalarystandardId())) {
			this.getHibernateTemplate().delete(salarystandard);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Salarystandard> findAll() {
		String hql = "from Salarystandard salarystandard where "
				+ "salarystandard.salary>=0";
		return this.getHibernateTemplate().find(hql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#findSalarystandardIdBySalarystandardName(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int findSalarystandardIdBySalarystandardName(
			String salarystandardName) {
		String hql = "select salarystandard.salarystandardId from "
				+ "Salarystandard salarystandard where "
				+ "salarystandard.salarystandardName=?";
		List<Integer> salarystandardIds = this.getHibernateTemplate().find(hql,
				salarystandardName);
		int salarystandardId = (null != salarystandardIds && 0 != salarystandardIds
				.size()) ? salarystandardIds.get(0).intValue() : 0;
		return salarystandardId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#get(int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Salarystandard get(int id) {
		String hql = "from Salarystandard salarystandard where "
				+ "salarystandard.salarystandardId=? and "
				+ "salarystandard.salary>=0";
		List<Salarystandard> salarystandards = this.getHibernateTemplate()
				.find(hql, new Integer(id));
		Salarystandard salarystandard = (null != salarystandards && 0 != salarystandards
				.size()) ? salarystandards.get(0) : null;
		return salarystandard;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#save(cn.edu.pku.model.Salarystandard)
	 */
	@Override
	public Serializable save(Salarystandard salarystandard) {
		if (0 == this.findSalarystandardIdBySalarystandardName(salarystandard
				.getSalarystandardName())) {
			this.getHibernateTemplate().save(salarystandard);
			salarystandard.setSalarystandardId(this
					.findSalarystandardIdBySalarystandardName(salarystandard
							.getSalarystandardName()));
		} else {
			salarystandard = null;
		}
		return salarystandard;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.pku.dao.SalarystandardDao#update(cn.edu.pku.model.Salarystandard)
	 */
	@Override
	public void update(Salarystandard salarystandard) {
		if (null != this.get(salarystandard.getSalarystandardId())) {
			// this.getHibernateTemplate().saveOrUpdate(salarystandard);
			this.getHibernateTemplate().update(salarystandard);
		}
	}

}
